<?php

namespace App\Kuafu\WaiMai\Controller\B;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Order\Enum\OrderCodeEnum;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Service\WaiMaiOrderService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class OrderController extends BaseController
{

    /**
     * 外卖订单列表
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $sourceType        = (int)$request->get('sourceType', 0);
        $orderStatus       = $request->orderStatus ?? WaiMaiDBEnum::ORDER_STATUS_TOTAL;
        $pageSize          = (int)$request->pageSize ?: 10;
        $pageNum           = (int)$request->pageNum ?: 1;
        $completeTimeStart = $request->completeTimeStart;
        $completeTimeEnd   = $request->completeTimeEnd;
        $shopId            = (int)$request->get('shopId');
        $orderId           = (string)$request->orderId;
        $export            = (bool)$request->get("export", false);
        $serialNum         = (int)$request->serialNum;
        if ($completeTimeStart && $completeTimeEnd && ($completeTimeEnd < $completeTimeStart)) {
            return $this->fail(OrderCodeEnum::ERR_ORDER_TIME, OrderCodeEnum::CODE_TEXT[OrderCodeEnum::ERR_ORDER_TIME]);
        }

        $this->validate($request, [
            'completeTimeStart' => 'required|date',
            'completeTimeEnd'   => 'required|date',
            'shopId'            => 'required|integer',
        ], [
            'completeTimeStart.required' => "开始日期不能为空",
            'completeTimeEnd.required'   => "结束日期不能为空",
            'shopId.required'            => "门店不能为空",
        ]);

        return $this->success(WaiMaiOrderService::waiMaiOrderList(
                $shopId, $completeTimeStart, $completeTimeEnd,
                $orderStatus, $sourceType, $pageSize, $pageNum, $orderId, $export, $serialNum
            ));
    }

    // 外卖订单详情
    public function detail(Request $request, $orderId): JsonResponse
    {
        $orderId = (int)$orderId;
        return $this->success(WaiMaiOrderService::orderDetail($orderId,1));
    }

}
